<?php
header('Content-type: text/html; charset=utf-8');
include_once 'module/conn.php';

$id = @ $_GET["id"];
$userId = $_GET["userId"];
$userPwd = $_GET["userPwd"];
$userSex = @ $_GET["userSex"];
$userSign = @ $_GET["userSign"];
$userMail = @ $_GET["userMail"];
$userBirthday = @ $_GET["userBirthday"];
$userDeviceId = @ $_GET["userDeviceId"];
$type = $_GET["type"];
$userIp = @ $_SERVER["REMOTE_ADDR"];

$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

if ($userBirthday == '') {
	$userBirthday = '0000-00-00';
}

if ($type == "l") {
	//用户登录
	$sql = "select * from live_user where userId = '" . $userId . "' and userPwd = '" . $userPwd . "'";
	$result = mysql_query($sql, $conn);
	if (mysql_num_rows($result) == 1) {
		while ($user = mysql_fetch_object($result)) {
			$userArray[] = $user;
		}
		//echo '{"sql":"' . $sql . '","error":"false","results":' . json_encode($userArray) . '}';
		echo '{"error":"false","results":' . json_encode($userArray) . '}';
	} else {
		echo '{"sql":"' . $sql . '","error":"true","results":"用户名或密码错误，请重新登录！"}';
	}
} else
	if ($type == "r") {
		//用户注册
		$sql = "select * from live_user where userId = '" . $userId . "'";
		$result = mysql_query($sql, $conn);
		//用户唯一性校验
		if (mysql_num_rows($result)) {
			echo '{"sql":"' . $sql . '","error":"true","results":"用户名已注册，请重新选择！"}';
		} else {
			$sql = "insert into live_user (userId, userPwd, userSex, userBirthday, userType, userIp, userReg) values('" . $userId . "', '" . $userPwd . "', '" . $userSex . "', '" . $userBirthday . "', '1', '" . $userIp . "' ,now())";
			mysql_query($sql, $conn);

			echo '{"sql":"' . $sql . '","error":"false","results":"success","id":"' . mysql_insert_id() . '"}';
		}
	} else
		if ($type == "e") {
			//用户编辑

			//头像上传
			$path = null;
			$userPic = file_get_contents('php://input');
			if ($userPic != null) {

				$path = "user/avatar/" . $id . ".jpg";
				file_put_contents($path, $userPic);
				if (!file_exists($path)) {
					echo '{"sql":"' . $sql . '","error":"false","results":"头像编辑失败，请重新编辑！"}';
					return;
				}
			}
			 
			//更新用户信息
			if ($userPwd == null) {
				if ($path != null) {
					$sql = "update live_user set userSex='" . $userSex . "', userMail='" . $userMail . "', userSign='" . $userSign . "', userPic='Y', userBirthday='" . $userBirthday . "' where userId='" . $userId . "'";
				}else{
					$sql = "update live_user set userSex='" . $userSex . "', userMail='" . $userMail . "', userSign='" . $userSign . "', userBirthday='" . $userBirthday . "' where userId='" . $userId . "'";
				}
			} else {
				if ($path != null) {
					$sql = "update live_user set userPwd='" . $userPwd . "', userSex='" . $userSex . "', userMail='" . $userMail . "', userSign='" . $userSign . "', userPic='Y', userBirthday='" . $userBirthday . "' where userId='" . $userId . "'";
				}else{
					$sql = "update live_user set userPwd='" . $userPwd . "', userSex='" . $userSex . "', userMail='" . $userMail . "', userSign='" . $userSign . "', userBirthday='" . $userBirthday . "' where userId='" . $userId . "'";
				}
			}
			if (mysql_query($sql, $conn)) {
				//echo '{"sql":"' . $sql . '","error":"true","results":"success"}';
				echo '{"error":"true","results":"success"}';
			} else {
				echo '{"sql":"' . $sql . '","error":"false","results":"用户信息编辑失败，请重新编辑！"}';
			}

		}
		


	
$userCity = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" . $userIp);
$userCity = explode("\t", $userCity);
if($userCity[4] == "" && $userCity[5] == ""){
	$userCity = $userCity[3];
}else{
	$userCity = $userCity[4] . ' ' . $userCity[5];
}
$sql = "update live_user set userIp = '".$userIp."', userDeviceId = '". $userDeviceId ."', userCity = '". iconv("gb2312", "UTF-8", $userCity) ."' where userId = '". $userId ."'";
mysql_query($sql, $conn);
?>